package com.mobile.check.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.kit.NLog;
import com.mobile.check.beans.TaskItemInfo;

public class ExportLeakPointUtil {

    private int mPageWidth;//px
    private int mPageHeight;//px
    private int mLineHeight;//px
    private int mMargin;//px
    private int mTableHeight;//px
    private int mTableWidth;//px
    private int mImageWidth;//px
    private int mImageHeight;//px


    private LineInfo lineInfo_h_0;
    private LineInfo lineInfo_h_1;
    private LineInfo lineInfo_h_2;
    private LineInfo lineInfo_h_3;
    private LineInfo lineInfo_h_4;
    private LineInfo lineInfo_h_5;
    private LineInfo lineInfo_h_6;
    private LineInfo lineInfo_h_7;
    private LineInfo lineInfo_h_8;
    private LineInfo lineInfo_h_9;
    private LineInfo lineInfo_h_10;
    private LineInfo lineInfo_h_11;


    private LineInfo lineInfo_v_0;
    private LineInfo lineInfo_v_1;
    private LineInfo lineInfo_v_2;
    private LineInfo lineInfo_v_3;
    private LineInfo lineInfo_v_4;
//    private LineInfo lineInfo_v_5;
    private LineInfo lineInfo_v_6;
    private LineInfo lineInfo_v_7;
    private LineInfo lineInfo_v_8;


    Bitmap mBitmap;
    MyCanvas mCanvas;
    Paint pLine;

    Context mContext;

    public ExportLeakPointUtil(Context context) {
        mContext = context;
        init();
    }

    private void init() {
        int lineHeight = 1;//px
        int margin = 40;//dp
        int tableHeight = 50;//dp
        int tableWidth = 120;//dp
        int imageHeight = 600;//dp
        int imageWidth = tableWidth * 5;//dp

        int pageHeight = tableHeight * 9 + margin * 2 + imageHeight;//dp
        int pageWidth = tableWidth * 6 + margin * 2;//dp
        mPageWidth = Kits.Dimens.dp2px(pageWidth);
        mPageHeight = Kits.Dimens.dp2px(pageHeight);


        mLineHeight = Kits.Dimens.dp2px(lineHeight);
        mMargin = Kits.Dimens.dp2px(margin);
        mTableHeight = Kits.Dimens.dp2px(tableHeight);
        mTableWidth = Kits.Dimens.dp2px(tableWidth);
        mImageWidth = Kits.Dimens.dp2px(imageWidth);
        mImageHeight = Kits.Dimens.dp2px(imageHeight);
    }

    public String startExport( TaskItemInfo itemInfo,String targetDir) {

        String newPicName = itemInfo.getTaskName() + "-" + itemInfo.getLabel() + ".jpeg";


        mBitmap = Bitmap.createBitmap(mPageWidth, mPageHeight, Bitmap.Config.RGB_565);
        mCanvas = new MyCanvas(mBitmap);
        pLine = new Paint();
        pLine.setAntiAlias(true);

        pLine.setStyle(Paint.Style.FILL);
        pLine.setColor(Color.rgb(255, 255, 255));
        mCanvas.drawRect(0, 0, mPageWidth, mPageHeight, pLine);
        drawLine();
        drawTxt(itemInfo);
        drawPic(itemInfo);
        mCanvas.save();

        String picPath = targetDir +
                "泄漏点" + "/图片/";

        Kits.File.makeDirs(picPath);
        File file = new File(picPath, newPicName);
        if (null != file && file.exists()) {
            file.delete();
        }
        saveBitmap(mBitmap, file);
        return file.getPath();
    }


    private void drawLine() {
        pLine.setStrokeWidth(mLineHeight);
        pLine.setColor(Color.rgb(0, 0, 0));

        float left = mMargin;
        float top = mMargin;
        float right = mPageWidth - mMargin;
        float bottom = mPageHeight - mMargin;
        int imageHeight = mImageHeight;

        lineInfo_h_0 = mCanvas.toDrawLine(left, right, top + mTableHeight * 0, top + mTableHeight * 0, pLine);
        lineInfo_h_1 = mCanvas.toDrawLine(left, right, top + mTableHeight * 1, top + mTableHeight * 1, pLine);
        lineInfo_h_2 = mCanvas.toDrawLine(left, right, top + mTableHeight * 2, top + mTableHeight * 2, pLine);
        lineInfo_h_3 = mCanvas.toDrawLine(left, right, top + mTableHeight * 3, top + mTableHeight * 3, pLine);
        lineInfo_h_4 = mCanvas.toDrawLine(left, right, top + mTableHeight * 4, top + mTableHeight * 4, pLine);
        lineInfo_h_5 = mCanvas.toDrawLine(left, right, top + mTableHeight * 5, top + mTableHeight * 5, pLine);
        lineInfo_h_6 = mCanvas.toDrawLine(left, right, top + mTableHeight * 6, top + mTableHeight * 6, pLine);

        float endY = top + mImageHeight;
        lineInfo_h_7 = mCanvas.toDrawLine(left, right, endY + mTableHeight * 6, endY + mTableHeight * 6, pLine);
        lineInfo_h_8 = mCanvas.toDrawLine(left, right, endY + mTableHeight * 7, endY + mTableHeight * 7, pLine);
        lineInfo_h_9 = mCanvas.toDrawLine(left, right, endY + mTableHeight * 8, endY + mTableHeight * 8, pLine);
        lineInfo_h_10 = mCanvas.toDrawLine(left, right, endY + mTableHeight * 9, endY + mTableHeight * 9, pLine);
//        for (int i = 0; i < 7; i++) {
//            mCanvas.drawLine(left, top + mTableHeight * i, right, top + mTableHeight * i, pLine);
//        }
//        pLine.setColor(Color.rgb(255, 0, 0));
//        for (int i = 0; i < 4; i++) {
//            float y = top + mTableHeight * (6 + i) + imageHeight;
//            mCanvas.drawLine(left, y, right, y, pLine);
//        }
//
//        mCanvas.save();
//        pLine.setColor(Color.rgb(0, 0, 0));

        lineInfo_v_0 = mCanvas.toDrawLine(left, left, top, bottom, pLine);
        lineInfo_v_1 = mCanvas.toDrawLine(left + mTableWidth, left + mTableWidth, top + mTableHeight, bottom, pLine);

        float width = (right - lineInfo_v_1.startX - lineInfo_v_0.startX)/4;


        lineInfo_v_2 = mCanvas.toDrawLine(lineInfo_v_1.startX+width, lineInfo_v_1.startX+width, top + mTableHeight * 3, top + mTableHeight * 5, pLine);
        lineInfo_v_3 = mCanvas.toDrawLine(lineInfo_v_2.startX+width, lineInfo_v_2.startX+width, top + mTableHeight * 3, top + mTableHeight * 5, pLine);
        lineInfo_v_4 = mCanvas.toDrawLine(lineInfo_v_3.startX+width, lineInfo_v_3.startX+width, top + mTableHeight * 3, top + mTableHeight * 5, pLine);
//        lineInfo_v_5 = mCanvas.toDrawLine(lineInfo_v_4.startX+width, lineInfo_v_4.startX+width, top + mTableHeight * 3, top + mTableHeight * 5, pLine);
        lineInfo_v_6 = mCanvas.toDrawLine(right, right, top, bottom, pLine);

        lineInfo_v_7 = mCanvas.toDrawLine(mPageWidth / 2, mPageWidth / 2, mTableHeight * 6 + mImageHeight + mMargin, bottom, pLine);
        lineInfo_v_8 = mCanvas.toDrawLine(mPageWidth / 2 + mTableWidth, mPageWidth / 2 + mTableWidth, mTableHeight * 6 + mImageHeight + mMargin, bottom, pLine);


//        mCanvas.drawLine(left, top, left, bottom, pLine);//左侧竖线
//        mCanvas.drawLine(left + mTableWidth * 1, top + mTableHeight, left + mTableWidth * 1, bottom, pLine);//左侧第二条竖线
//        mCanvas.drawLine(left + mTableWidth * 2, top + mTableHeight * 3, left + mTableWidth * 2, top + mTableHeight * 5, pLine);//左侧第三条竖线
//        mCanvas.drawLine(left + mTableWidth * 3, top + mTableHeight * 3, left + mTableWidth * 3, top + mTableHeight * 5, pLine);//左侧第四条竖线
//
//        mCanvas.drawLine(left + mTableWidth * 4, top + mTableHeight * 3, left + mTableWidth * 4, top + mTableHeight * 5, pLine);//左侧第五条竖线
//        mCanvas.drawLine(left + mTableWidth * 5, top + mTableHeight * 3, left + mTableWidth * 5, top + mTableHeight * 5, pLine);//左侧第六条竖线
//
//        mCanvas.drawLine(mPageWidth / 2, mTableHeight * 6 + mImageHeight + mMargin, mPageWidth / 2, bottom, pLine);//
//        mCanvas.drawLine(mPageWidth / 2 + mTableWidth, mTableHeight * 6 + mImageHeight + mMargin, mPageWidth / 2 + mTableWidth, bottom, pLine);//
//        mCanvas.drawLine(right, top, right, bottom, pLine);//右侧竖线

    }

    private void drawTxt(TaskItemInfo itemInfo) {
        float left = mMargin;
        float top = mMargin;
        float right = mPageWidth - mMargin;
        Paint textPaint = new Paint();
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setStrokeWidth(8);
        textPaint.setTextSize(50);
        textPaint.setTextAlign(Paint.Align.CENTER);


        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;

        mCanvas.toDrawText("LDAR维修工单", lineInfo_v_0.startX, lineInfo_h_0.startY, lineInfo_v_6.endX, lineInfo_h_1.endY, distance, textPaint);
        textPaint.setTextSize(48);
        mCanvas.toDrawText("企业名称", lineInfo_v_0.startX, lineInfo_h_1.startY, lineInfo_v_1.startX, lineInfo_h_2.startY, distance, textPaint);
        mCanvas.toDrawText(itemInfo.getTaskName(), lineInfo_v_1.startX, lineInfo_v_1.startY, lineInfo_v_6.startX, lineInfo_h_2.startY, distance, textPaint);


        mCanvas.toDrawText("装置名称", lineInfo_v_0.startX, lineInfo_h_2.startY, lineInfo_v_1.startX, lineInfo_h_3.startY, distance, textPaint);
        mCanvas.toDrawText(itemInfo.getDevice(), lineInfo_v_1.startX, lineInfo_h_2.startY, right, lineInfo_h_3.startY, distance, textPaint);

        mCanvas.toDrawText("标签号", lineInfo_v_0.startX, lineInfo_h_3.startY, lineInfo_v_1.startX, lineInfo_h_4.startY, distance, textPaint);
        mCanvas.toDrawText(itemInfo.getLabel(), lineInfo_v_0.startX, lineInfo_h_4.startY, lineInfo_h_4.startX + mTableWidth, lineInfo_h_5.startY, distance, textPaint);

        mCanvas.toDrawText("组件类型", lineInfo_v_1.startX, lineInfo_h_3.startY, lineInfo_v_2.startX, lineInfo_h_4.startY, distance, textPaint);
        mCanvas.toDrawText(itemInfo.getAssemblyType(), lineInfo_v_1.startX, lineInfo_h_4.startY, lineInfo_v_2.startX, lineInfo_h_5.startY, distance, textPaint);

        mCanvas.toDrawText("泄漏部位", lineInfo_v_2.startX, lineInfo_h_3.startY, lineInfo_v_3.startX, lineInfo_h_4.startY, distance, textPaint);
        String leakLocation = itemInfo.getLeakLocation();
        if (itemInfo.getIsLeak().equals("0") || itemInfo.getIsLeak().equals("否")) {
            leakLocation = "";
        }
        mCanvas.toDrawText(leakLocation, lineInfo_v_2.startX, lineInfo_h_4.startY, lineInfo_v_3.startX, lineInfo_h_5.startY, distance, textPaint);


        mCanvas.toDrawText("检测值/ppm", lineInfo_v_3.startX, lineInfo_h_3.startY, lineInfo_v_4.startX, lineInfo_h_4.startY, distance, textPaint);
        mCanvas.toDrawText(itemInfo.getTestValue(), lineInfo_v_3.startX, lineInfo_h_4.startY, lineInfo_v_4.startX, lineInfo_h_5.startY, distance, textPaint);


//        mCanvas.toDrawText("修复期限/天", lineInfo_v_4.startX, lineInfo_h_3.startY, lineInfo_v_5.startX, lineInfo_h_4.startY, distance, textPaint);
//        mCanvas.toDrawText("7天", lineInfo_v_4.startX, lineInfo_h_4.startY, lineInfo_v_5.startX, lineInfo_h_5.startY, distance, textPaint);


        mCanvas.toDrawText("检测人员", lineInfo_v_4.startX, lineInfo_h_3.startY, lineInfo_v_6.startX, lineInfo_h_4.startY, distance, textPaint);
        mCanvas.toDrawText(itemInfo.getTestPerson(), lineInfo_v_4.startX, lineInfo_h_4.startY, lineInfo_v_6.startX, lineInfo_h_5.startY, distance, textPaint);


        mCanvas.toDrawText("位置描述", lineInfo_v_0.startX, lineInfo_h_5.startY, lineInfo_v_1.startX, lineInfo_h_6.startY, distance, textPaint);
        mCanvas.toDrawText(itemInfo.getLocationDesc(), lineInfo_v_1.startX, lineInfo_h_5.startY, lineInfo_v_6.startX, lineInfo_h_6.startY, distance, textPaint);

        mCanvas.toDrawText("泄漏照片", lineInfo_v_0.startX, lineInfo_h_6.startY, lineInfo_v_1.startX, lineInfo_h_7.startY, distance, textPaint);

        mCanvas.toDrawText("维修日期", lineInfo_v_0.startX, lineInfo_h_7.startY, lineInfo_v_1.startX, lineInfo_h_8.startY, distance, textPaint);
        mCanvas.toDrawText(itemInfo.getRepairDate(), lineInfo_v_1.startX, lineInfo_h_7.startY, lineInfo_v_7.startX, lineInfo_h_8.startY, distance, textPaint);

        mCanvas.toDrawText("维修方法", lineInfo_v_0.startX, lineInfo_h_8.startY, lineInfo_v_1.startX, lineInfo_h_8.startY + mTableHeight, distance, textPaint);
        mCanvas.toDrawText(itemInfo.getRepairMethed(), lineInfo_v_1.startX, lineInfo_h_8.startY, lineInfo_v_7.startX, lineInfo_h_9.startY, distance, textPaint);

        mCanvas.toDrawText("维修人员", lineInfo_v_0.startX, lineInfo_h_9.startY, lineInfo_v_1.startX, lineInfo_h_10.startY, distance, textPaint);
        mCanvas.toDrawText(itemInfo.getRepairPerson(), lineInfo_v_1.startX, lineInfo_h_9.startY, lineInfo_v_7.startX, lineInfo_h_10.startY, distance, textPaint);

        mCanvas.toDrawText("复测日期", lineInfo_v_7.startX, lineInfo_v_7.startY, lineInfo_v_7.startX + mTableWidth, lineInfo_v_7.startY + mTableHeight, distance, textPaint);
        mCanvas.toDrawText(itemInfo.getRepeatDate(), lineInfo_v_8.startX, lineInfo_h_7.startY, lineInfo_v_6.startX, lineInfo_h_8.startY, distance, textPaint);

        mCanvas.toDrawText("复测值", lineInfo_v_7.startX, lineInfo_h_8.startY, lineInfo_v_7.startX + mTableWidth, lineInfo_h_8.startY + mTableHeight, distance, textPaint);
        mCanvas.toDrawText(itemInfo.getRepeatValue(), lineInfo_v_8.startX, lineInfo_h_8.startY, lineInfo_v_6.startX, lineInfo_h_9.startY, distance, textPaint);

        mCanvas.toDrawText("复测人员", lineInfo_v_7.startX, lineInfo_h_9.startY, lineInfo_v_7.startX + mTableWidth, lineInfo_h_9.startY + mTableHeight, distance, textPaint);
        mCanvas.toDrawText(itemInfo.getRepeatPerson(), lineInfo_v_8.startX, lineInfo_h_9.startY, lineInfo_v_6.startX, lineInfo_h_10.startY, distance, textPaint);

    }


    private void drawPic(TaskItemInfo itemInfo) {

        String sRoot = Environment.getExternalStorageDirectory().getPath();// 获取跟目录
        String picPath = sRoot.concat("/").concat("检测系统") + File.separator
                + "本地任务" + File.separator
                + "图片"
                + File.separator
                + itemInfo.getTestPic();

        if (!Kits.File.isFileExist(picPath)) {
            return;

        }
        Bitmap initBitmap = getBitmapFromUri(mContext, picPath);
        int width = initBitmap.getWidth();
        int height = initBitmap.getHeight();

        float dx = width * 1.0f / height;
        int dstWidth;
        int dstHeight;
        if (width > mImageWidth) {
            dstWidth = mImageWidth;
            dstHeight = (int) (mImageWidth / dx);
        } else {
            dstWidth = width;
            dstHeight = height;
        }

        if (dstHeight > mImageHeight) {
            dstHeight = mImageHeight;
            dstWidth = (int) (dstHeight * dx);
        }
        dstHeight = dstHeight - mMargin / 2;

        Bitmap resourceBitmap = Bitmap.createScaledBitmap(initBitmap, dstWidth, dstHeight, true);
        initBitmap.recycle();
        int left = mPageWidth - mMargin - mImageWidth / 2 - dstWidth / 2;
        int top = mPageHeight - mMargin - 3 * mTableHeight - dstHeight / 2 - mImageHeight / 2;
        mCanvas.drawBitmap(resourceBitmap, left, top, null);
        resourceBitmap.recycle();
    }


    public Bitmap getBitmapFromUri(Context context, String outfile) {
        try {
            outfile = outfile.replace("/storage/emulated/0/", "file:///sdcard/");
            Uri uri = Uri.parse(outfile);
            // 读取uri所在的图片
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
            return bitmap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean saveBitmap(Bitmap bm, File f) {
        NLog.e("ExportUtil", "saveBitmap =");
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            bm.recycle();
        }
        return false;
    }


    public class LineInfo {

        public float startX, endX, startY, endY;

        public LineInfo(float startX, float endX, float startY, float endY) {
            this.startX = startX;
            this.endX = endX;
            this.startY = startY;
            this.endY = endY;
        }

        @Override
        public String toString() {
            return "{" +
                    "startX=" + startX +
                    ", endX=" + endX +
                    ", startY=" + startY +
                    ", endY=" + endY +
                    '}';
        }
    }

    class MyCanvas extends Canvas {
        int index = 0;

        public MyCanvas(@NonNull Bitmap bitmap) {
            super(bitmap);
        }

        public LineInfo toDrawLine(float startX, float endX, float startY, float endY, Paint paint) {
            LineInfo lineInfo = new LineInfo(startX, endX, startY, endY);
            NLog.d("toDrawLine", "line" + index + "=" + lineInfo.toString());
            super.drawLine(startX, startY, endX, endY, paint);
            index++;
            return lineInfo;
        }

        public void toDrawText(String text, float left, float top, float right, float bottom, float distance, Paint textPaint) {
            RectF rectF = new RectF(left, top, right, bottom);

            if (!TextUtils.isEmpty(text)) {
                drawText(text, rectF.centerX(), rectF.centerY() + distance, textPaint);
            }
        }
    }
}
